<template>
  <div class='box'>
     <!-- 表头 -->
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <span class="xueke">标签名称</span><el-form-item>
        <el-input
        @input="changeKeyword"
        v-model="queryObj.keyword"
        ></el-input>
      </el-form-item>
       <el-form-item>
        <span class="zhuangtai">状态:</span>
   <el-select v-model="value" placeholder="请选择">
     <el-option :value="1" label="启用"> </el-option>
     <el-option :value="0" label="禁用"> </el-option>
  </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="" @click="resetInput">清空</el-button>
        <el-button type="primary" @click="searchName">搜索</el-button>
      </el-form-item>
      <el-button type="success" class="right" @click="addSub('add')"><i class="el-icon-edit"></i>新增标签</el-button>
      <el-alert
    :title="'共'+`${total}`+'条记录'"
    type="info"
    show-icon>
  </el-alert>
    </el-form>
     <!-- 放置table组件 -->
      <el-table v-loading="loading" :data="list" rule>
        <!-- 放置列 -->
        <el-table-column  align="center" label="序号" type="index"/>
        <el-table-column prop="subjectName" align="center" width="200" label="所属学科" />
        <el-table-column prop="tagName" align="center" label="标签名称" />
        <el-table-column prop="username" align="center" label="创建者" />
        <el-table-column prop="addDate" align="center" label="创建日期" />
        <el-table-column prop="state" align="center" label="状态" >
          <template v-slot="{ row }">
              <span >{{ row.state ? '已启用' : '已禁用'}}</span>
            </template>
        </el-table-column>
        <el-table-column align="center" label="操作" width="240">
          <template #default="{row}">
            <el-button size="mini" type="text" @click="qiyong(row)" v-if="row.state">禁用</el-button>
            <el-button size="mini" type="text" @click="jinyong(row)" v-else>启用</el-button>
            <el-button size="mini" type="text" @click="addSub('edit',row)" :disabled="Boolean(row.state)">修改</el-button>
            <el-button size="mini" type="text" @click="delDir(row)" :disabled="Boolean(row.state)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
       <!-- 放置分页组件 -->
      <el-row type="flex" style="height:60px" align="middle" justify="end">
        <!-- 放置分页组件 -->
        <el-pagination
          :current-page="queryObj.page"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="queryObj.pagesize"
          layout=" prev, pager, next,sizes, jumper"
          :total="total"
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </el-row>

      <!-- dialog -->
    <el-dialog width="500px" :title="Title" :visible.sync="showDialog" :before-close="closeDia">
      <!-- 表单内容 -->
      <el-form ref="form" :model="subObj" :rules="rules" label-width="120px">
        <el-form-item prop="subjectID" label="目录名称">
         <template>
          <el-select v-model="subObj.subjectID" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </template>
        </el-form-item>
        <el-form-item prop="name" label="目录名称">
          <el-input v-model="subObj.tagName" style="width:300px" size="mini" placeholder="请输入目录名称"/>
        </el-form-item>
        <el-form-item>
          <el-row type="flex" justify="center">
            <el-col :span="12">
              <el-button size="mini" @click="closeDia">取消</el-button>
              <el-button type="primary" size="mini" @click="addRole">确定</el-button>
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { list, remove, detail, update, add, changeState } from '@/api/hmmm/tags.js'
import { simple } from '@/api/hmmm/subjects.js'
export default {
  data () {
    return {
      timer: null,
      drawerType: 'add',
      formInline: {
        user: '',
        region: ''
      },
      id: 0,
      list: [],
      showDialog: false,
      loading: false,
      total: null,
      queryObj: {
        page: 1,
        pagesize: 10,
        keyword: ''
      },
      form: {
        name: '',
        state: 0,
        description: ''
      },
      options: [],
      value: '',
      rules: {
        directoryName: [{ required: true, message: '请输入学科名称', trigger: 'blur' }]
      },
      subObj: {
        subjectID: '',
        tagName: '',
        subjectName: ''
      }
    }
  },
  // 用户列表
  async created () {
    // 获取下拉列表
    this.getDicList()
    const { data } = await simple()
    // console.log(data)
    this.options = data
  },
  methods: {
    // 封装渲染函数
    async getDicList () {
      const { data } = await list(this.queryObj)
      // console.log(data)
      this.total = data.counts
      this.list = [...data.items]
      // console.log(this.list)
      this.list = data.items.map(item => {
        item.addDate = item.addDate.substring(0, 10) + '' + item.addDate.substring(11, 19)
        return item
      })
    },
    // 搜索功能
    async searchName () {
      // this.list = this.list.filter(item => item.tagName === uname)
      const { data } = await list({ ...this.queryObj, state: this.value })
      this.list = [...data.items]
    },
    // 启用
    async qiyong (id) {
      id.state = 0
      await changeState({ id: id.id, state: id.state })
      this.$message.success('操作成功')
    },
    // 禁用
    async jinyong (id) {
      id.state = 1
      await changeState({ id: id.id, state: id.state })
      this.$message.success('操作成功')
    },
    changeKeyword () {
      clearTimeout(this.timer) // 清理上一次的定时器
      this.timer = setTimeout(() => {
        this.queryObj.page = 1
        this.getDicList()
      }, 300)
    },
    // 清空功能
    resetInput () {
      this.formInline.user = ''
      this.getDicList()
    },
    // 分页
    handleSizeChange (pagesize) {
      this.queryObj.pagesize = pagesize
      this.getDicList()
    },
    handleCurrentChange (page) {
      this.queryObj.page = page
      this.getDicList()
    },
    // 删除
    async delDir (id) {
      try {
        await remove(id)
        this.$message.success('删除成功！')
      } catch (error) {
        this.$message.error('删除失败！')
      }
      this.getDicList()
    },
    // 展示dialog
    async addSub (type, id) {
      // console.log('add')
      this.drawerType = type
      if (type === 'edit') {
        const { data } = await detail(id)
        console.log(data)
        this.subObj.subjectID = data.subjectID
        this.subObj.tagName = data.tagName
        //
        this.subObj.subjectName = data.subjectName
        // this.subObj.subjectID = data.subjectID
        this.id = data.id
      }
      this.showDialog = true
    },
    // 关闭dialog弹窗
    closeDia () {
      this.subObj.subjectID = ''
      this.subObj.tagName = ''
      //
      this.subObj.subjectName = ''
      this.id = ''
      this.showDialog = false // 关闭弹层
    },
    // 发起新增请求
    async addRole () {
      try {
        if (this.drawerType === 'add') {
          // 添加
          await add(this.subObj)
        }
        // 修改
        if (this.drawerType === 'edit') {
          await update({ id: this.id, ...this.subObj })
        }
        // 清空表单
        this.subObj.subjectID = ''
        this.subObj.tagName = ''
        // 轻提示
        this.$message.success(
          this.drawerType === 'add' ? '添加成功' : '修改成功'
        )
        this.getDicList()
        this.showDialog = false
      } catch (error) {
        this.$message.error('失败')
      }
    }
  },
  computed: {
    Title () {
      if (this.drawerType === 'add') return '新增学科'
      if (this.drawerType === 'edit') return '修改学科'
      else {
        return 'add'
      }
    }
  }
}
</script>

<style scoped lang="less">
.box {
  background-color: #fff;
  padding: 20px 20px 70px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  position: relative;
  //编辑、删除按钮样式
  .bt1 {
    background-color: #ecf5ff;
    color: #409eff;
  }
  .bt1:hover {
    background-color: #409eff;
    color: #ecf5ff;
  }
  .bt2 {
    background-color: #ecf5ff;
    color: #f87f92;
  }
  .bt2:hover {
    background-color: #f87f92;
    color: #ecf5ff;
  }
  .right {
    float: right;
  }
  .fpage {
    position: absolute;
    right: 30px;
    bottom: 20px;
  }
  .xueke{
    display: inline-block;
    margin-top: 9px;
    margin-left: 15px;
    margin-right: 15px;
  }
  .zhuangtai{
    margin-left: 25px;
    margin-right: 10px;
  }
}
</style>
